#include "GuiDebugWindow.h"
#include "GameManager.h"
#include "NavigationManager.h"

#include <vector>

CGuiDebugWindow::CGuiDebugWindow(void) {
	CGameManager *engine = CGameManager::getInstance();

	IGUIWindow* window = engine->getGuiManager()->addWindow(rect<s32>(100, 100, 500, 500), false, L"Test window");
	IGUIButton* changeHeading = engine->getGuiManager()->addButton(rect<s32>(position2d<s32>(0,25), dimension2d<s32>(75,25)), window, 123, L"Change heading");

	mXAxis = engine->getGuiManager()->addEditBox(L"", rect<s32>(position2d<s32>(0,50), dimension2d<s32>(30,25)), true, window);
	mYAxis = engine->getGuiManager()->addEditBox(L"", rect<s32>(position2d<s32>(35,50), dimension2d<s32>(30,25)), true, window);
	
	IGUIButton* changeAcceleration = engine->getGuiManager()->addButton(rect<s32>(position2d<s32>(100,25), dimension2d<s32>(75,25)), window, 432, L"Change speed");
	mAcceleration = engine->getGuiManager()->addEditBox(L"", rect<s32>(position2d<s32>(100,50), dimension2d<s32>(30,25)), true, window);
	
	IGUIButton* fireButton = engine->getGuiManager()->addButton(rect<s32>(position2d<s32>(200,25), dimension2d<s32>(75,25)), window, 432, L"FIRE!");

	IGUIButton* navProjectButton = engine->getGuiManager()->addButton(rect<s32>(position2d<s32>(200,75), dimension2d<s32>(100,25)), window, 432, L"Plot location");
	IGUIButton* navCourseButton = engine->getGuiManager()->addButton(rect<s32>(position2d<s32>(200,100), dimension2d<s32>(100,25)), window, 432, L"Plot Course");
	IGUIButton* navPrepareInterceptButton = engine->getGuiManager()->addButton(rect<s32>(position2d<s32>(200,150), dimension2d<s32>(100,25)), window, 432, L"Prepare intercept");
	IGUIButton* navInterceptButton = engine->getGuiManager()->addButton(rect<s32>(position2d<s32>(200,175), dimension2d<s32>(100,25)), window, 432, L"Plot intercept");
	IGUIButton* navZeroInterceptButton = engine->getGuiManager()->addButton(rect<s32>(position2d<s32>(200,200), dimension2d<s32>(100,25)), window, 432, L"Plot 0/0 intercept");


	addGuiEventT<CGuiDebugWindow>(engine->getEventManager(), EGUI_EVENT_TYPE::EGET_BUTTON_CLICKED, changeAcceleration, &CGuiDebugWindow::onAccelerationChange, this, NULL, 1);
	addGuiEventT<CGuiDebugWindow>(engine->getEventManager(), EGUI_EVENT_TYPE::EGET_BUTTON_CLICKED, changeHeading, &CGuiDebugWindow::onHeadingChange, this, NULL, 2);
	addGuiEventT<CGuiDebugWindow>(engine->getEventManager(), EGUI_EVENT_TYPE::EGET_BUTTON_CLICKED, fireButton, &CGuiDebugWindow::onFireButton, this, NULL, 3);
	addGuiEventT<CGuiDebugWindow>(engine->getEventManager(), EGUI_EVENT_TYPE::EGET_BUTTON_CLICKED, navProjectButton, &CGuiDebugWindow::onNavigationProjectPosition, this, NULL, 4);
	addGuiEventT<CGuiDebugWindow>(engine->getEventManager(), EGUI_EVENT_TYPE::EGET_BUTTON_CLICKED, navCourseButton, &CGuiDebugWindow::onNavigationProjectCourse, this, NULL, 5);
	addGuiEventT<CGuiDebugWindow>(engine->getEventManager(), EGUI_EVENT_TYPE::EGET_BUTTON_CLICKED, navPrepareInterceptButton, &CGuiDebugWindow::onNavigationPrepareIntercept, this, NULL, 5);
	addGuiEventT<CGuiDebugWindow>(engine->getEventManager(), EGUI_EVENT_TYPE::EGET_BUTTON_CLICKED, navInterceptButton, &CGuiDebugWindow::onNavigationProjectIntercept, this, NULL, 5);
	addGuiEventT<CGuiDebugWindow>(engine->getEventManager(), EGUI_EVENT_TYPE::EGET_BUTTON_CLICKED, navZeroInterceptButton, &CGuiDebugWindow::onNavigationZeroIntercept, this, NULL, 5);
}

CGuiDebugWindow::~CGuiDebugWindow(void)
{

}
bool CGuiDebugWindow::onNavigationPrepareIntercept(const SEvent& e, IEventRecEx *ex) {
	CGameManager *engine = CGameManager::getInstance();
	CSceneObject *src = NULL, *target = NULL;
	vector3df res;

	int x = 0;
	OBJINTMAP map = CGameManager::getInstance()->getObjects();
	for(OBJINTMAP::iterator iter = map.begin(); iter != map.end() && x < 2; iter++) {
		if(iter->second->getType() == E_GAMEOBJECT_TYPE::Ship) {
			if(x==0) 
				src = iter->second;
			else 
				target = iter->second;
			x++;
		}	
	}

	target->setBearing(0, 90);
	matrix4 m;
	vector3df vel;
	target->setAcceleration(100);

	return false;
}
bool CGuiDebugWindow::onNavigationProjectIntercept(const SEvent& e, IEventRecEx *ex) { 
	CGameManager *engine = CGameManager::getInstance();
	CSceneObject *src = NULL, *target = NULL;
	vector3df res;

	int x = 0;
	OBJINTMAP map = CGameManager::getInstance()->getObjects();
	for(OBJINTMAP::iterator iter = map.begin(); iter != map.end() && x < 2; iter++) {
		if(iter->second->getType() == E_GAMEOBJECT_TYPE::Ship) {
			if(x==0) 
				src = iter->second;
			else 
				target = iter->second;
			x++;
		}	
	}

	CNavigationManager *nav = new CNavigationManager();

	std::vector<vector3df> interceptCourses;
	std::vector<f32>		times;
	bool canIntercept = nav->getInterceptCourse(src, target, interceptCourses, times, 500);

	for(unsigned int i = 0; i < times.size(); i++) {
		//interceptLocations[i] = nav->getProjectedPosition(target, interceptLocations[i].X);
		ISceneNode *s = engine->getSceneManager()->addCubeSceneNode(75, 0, -1, nav->getProjectedPosition(target, times[i]));
		s->getMaterial(0).EmissiveColor.set(0,0,255,0);
		//DEBUG_PRINTVECT("got position", interceptLocations[i]);
	}

	// now plot a square there
	//ISceneNode *s = engine->getSceneManager()->addCubeSceneNode(75, 0, -1, interceptLocation);
	//s->getMaterial(0).EmissiveColor.set(0,0,255,0);
	src->setRotation(interceptCourses[0]);
	src->setAcceleration(500);

	delete nav;

	return false;
}
bool CGuiDebugWindow::onNavigationZeroIntercept(const SEvent& e, IEventRecEx *ex){
	CGameManager *engine = CGameManager::getInstance();
	CSceneObject *src = NULL, *target = NULL;
	vector3df res;

	int x = 0;
	OBJINTMAP map = CGameManager::getInstance()->getObjects();
	for(OBJINTMAP::iterator iter = map.begin(); iter != map.end() && x < 2; iter++) {
		if(iter->second->getType() == E_GAMEOBJECT_TYPE::Ship) {
			if(x==0) 
				src = iter->second;
			else 
				target = iter->second;
			x++;
		}	
	}

	CNavigationManager *nav = new CNavigationManager();

	std::vector<vector3df> interceptCourses;
	std::vector<f32>		times;
	bool canIntercept = nav->getInterceptCourse(src, target, interceptCourses, times, 500);

	for(unsigned int i = 0; i < times.size(); i++) {
		//interceptLocations[i] = nav->getProjectedPosition(target, interceptLocations[i].X);
		ISceneNode *s = engine->getSceneManager()->addCubeSceneNode(75, 0, -1, nav->getProjectedPosition(target, times[i]));
		s->getMaterial(0).EmissiveColor.set(0,0,255,0);
		//DEBUG_PRINTVECT("got position", interceptLocations[i]);
	}

	// now plot a square there
	//ISceneNode *s = engine->getSceneManager()->addCubeSceneNode(75, 0, -1, interceptLocation);
	//s->getMaterial(0).EmissiveColor.set(0,0,255,0);
	DEBUG_PRINTVECT("Intercept Course:", getYXangle(interceptCourses[0]));

	delete nav;

	return false;
}
bool CGuiDebugWindow::onNavigationProjectCourse(const SEvent& e, IEventRecEx *ex) { 
	CGameManager *engine = CGameManager::getInstance();
	CSceneObject *obj;
	CShip *ptr;

	// first, move the selected ship
	obj = engine->getSelection()[0];
	if(obj->getType() != E_GAMEOBJECT_TYPE::Ship) 
		return false;

	ptr = (CShip *)obj;

	CNavigationManager *nav = new CNavigationManager();
	
	std::vector<vector3df> course = nav->getProjectedCourse(ptr, 10);

	DEBUG_PRINTVECT("current:", ptr->getPosition());
	
	for(unsigned int x=0;x<course.size();x++) {
		DEBUG_PRINTVECT("x:", course[x]);
		ISceneNode *s = engine->getSceneManager()->addCubeSceneNode(5.0f, NULL, -1, course[x]);
		s->getMaterial(0).EmissiveColor.set(0,0,255,0);
	}
	delete nav;

	return false;
}
bool CGuiDebugWindow::onNavigationProjectPosition(const SEvent& e, IEventRecEx *ex) { 
	CGameManager *engine = CGameManager::getInstance();
	CSceneObject *obj;
	CShip *ptr;

	// first, move the selected ship
	obj = engine->getSelection()[0];
	if(obj->getType() != E_GAMEOBJECT_TYPE::Ship) 
		return false;

	ptr = (CShip *)obj;

	CNavigationManager *nav = new CNavigationManager();
	
	vector3df projPos = nav->getProjectedPosition(ptr, 5);

	DEBUG_PRINTVECT("current:", ptr->getPosition());
	DEBUG_PRINTVECT("projected:", projPos);

	// now plot a square there
	ISceneNode *s = engine->getSceneManager()->addCubeSceneNode(75, 0, -1, projPos);
	s->getMaterial(0).EmissiveColor.set(0,0,255,0);

	delete nav;

	return false;
}
bool CGuiDebugWindow::onFireButton(const SEvent& e, IEventRecEx *ex) {
	CGameManager *engine = CGameManager::getInstance();
	CSceneObject *obj;
	CShip *ptr;

	if(engine->getSelection().size() == 0)
		return false;

	obj = engine->getSelection()[0];
	if(obj->getType() == E_GAMEOBJECT_TYPE::Ship) { 
		ptr = (CShip *)obj;
		ptr->fireFullMount(E_WEAPON_MOUNTPOINT::Left);
	}

	return false;
}
bool CGuiDebugWindow::onAccelerationChange (const SEvent& e,IEventRecEx* ex) {
	CGameManager *engine = CGameManager::getInstance();
	CShip *ptr;
	CSceneObject *obj;
	
	if(engine->getSelection().size() == 0)
		return false;


	obj = engine->getSelection()[0];
	if(obj->getType() == E_GAMEOBJECT_TYPE::Ship) { 
		ptr = (CShip *)obj;
		
		ptr->setAcceleration((f32)_wtof(mAcceleration->getText()));

	}

	return false;
}
bool CGuiDebugWindow::onHeadingChange (const SEvent& e,IEventRecEx* ex) {
	CGameManager *engine = CGameManager::getInstance();
	CShip *ptr;
	CSceneObject *obj;

	if(engine->getSelection().size() == 0)
		return false;


	obj = engine->getSelection()[0];
	if(obj->getType() == E_GAMEOBJECT_TYPE::Ship) { 
		ptr = (CShip *)obj;
		obj->setBearing( (f32)_wtof(mXAxis->getText()), (f32)_wtof(mYAxis->getText()));
	}

	return false;
}
